package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.dto.DataOverViewQueryDTO;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        //今天的总订单数
        Integer totalCount = orderMapper.getEveryDayCount(map);

        //新增用户数
        Integer newUsers = userMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        //有效订单数
        Integer validOrderCount = orderMapper.getEveryDayCount(map);

        //订单完成率
        Double orderCompletionRate = 0.0;
        if(totalCount != 0){
            orderCompletionRate = validOrderCount.doubleValue() / totalCount;
        }

        //平均客单价 select avg(amount) from orders where status = 5
        Double unitPrice = orderMapper.avgPrice(map);
        if(unitPrice == null){
            unitPrice = 0.0;
        }
        //保留两位小数
        unitPrice = new BigDecimal(unitPrice).setScale(2, RoundingMode.HALF_UP).doubleValue();


        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {

        //根据状态查询已停售和已起售的套餐数量
        Integer sold = setmealMapper.getStatusCount(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.getStatusCount(StatusConstant.DISABLE);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {

        Integer discontinued = dishMapper.getStatusCount(StatusConstant.DISABLE);
        Integer sold = dishMapper.getStatusCount(StatusConstant.ENABLE);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {

        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);

        Map map = new HashMap<>();
        map.put("begin",beginTime);
        //根据不同状态查询不同状态的数量
        //已取消订单数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.getStatusCount(map);
        //已完成订单数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.getStatusCount(map);
        //派送中订单数量
        map.put("status",Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = orderMapper.getStatusCount(map);
        //待接单订单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getStatusCount(map);
        //订单数量总和
        Integer allOrders = cancelledOrders + completedOrders + deliveredOrders + waitingOrders;


        return OrderOverViewVO.builder()
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .allOrders(allOrders)
                .build();
    }
}
